package org.example.rmsmsapi.netManager;

import okhttp3.*;

import java.util.concurrent.TimeUnit;

/**
 * Created by 黄启位 on 2022-04-04.
 */
public class POkHttpManger {
 public static OkHttpClient GetOkhttpclient(){
    OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
            .connectTimeout(20, TimeUnit.SECONDS)//连接时间
            .readTimeout(20, TimeUnit.SECONDS)//读时间
            .writeTimeout(20, TimeUnit.SECONDS)//写时间
            .retryOnConnectionFailure(true)//连接失败后是否重新连接
//                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())//配置
//                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())//配置
          //  .hostnameVerifier(new OkHttpHostnameVerifier())
            .build();
    return mOkHttpClient;
 }
public static RequestBody okReqBody(String json){
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
    return  requestBody;
}


public static   Request okReqest( RequestBody requestBody,String URL){
    Request request = new Request.Builder()
            .url(URL)
            .post(requestBody )
            .build();
    //System.out.print(plat_url+URL+"<<");
    return request;
}


    public static   Request okReqest( RequestBody requestBody,String URL,String token){
        Request request = new Request.Builder()
                .addHeader("token",token)
                .url(URL)
                .post(requestBody )
                .build();
        //System.out.print(plat_url+URL+"<<");
        return request;
    }
    /****
     * 返回Call用于Https请求
     * @param reqString
     * @return
     */
    public static Call OkCall(String reqString,String URL){
    OkHttpClient mOkHttpClient= POkHttpManger.GetOkhttpclient();
    RequestBody requestBody= POkHttpManger.okReqBody(reqString);
    Request request= POkHttpManger.okReqest(requestBody,URL);
    Call call = mOkHttpClient.newCall(request);
    return call;
}


    public static Call OkCall(String reqString,String URL,String token){
        OkHttpClient mOkHttpClient= POkHttpManger.GetOkhttpclient();
        RequestBody requestBody= POkHttpManger.okReqBody(reqString);
        Request request= POkHttpManger.okReqest(requestBody,URL,token);
        Call call = mOkHttpClient.newCall(request);
        return call;
    }
}
